﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace DbManager.Query
{
    public class SqlQuery<T> : SqlQuery
    {
        private bool _disposeDbManager = true;
        public virtual bool DisposeDbManager
        {
            get { return _disposeDbManager; }
            set { _disposeDbManager = value; }
        }
        
        private static readonly Hashtable _actionSqlQueryInfo = new Hashtable();
        public SqlQueryInfo GetSqlQueryInfo(DbManager db, Type type, string actionName)
        {
            string key = type.FullName + "$" + actionName + "$" + db.DataProvider.UniqueName + 
                "$" + GetTableName(type);
            SqlQueryInfo query = (SqlQueryInfo)_actionSqlQueryInfo[key];

            if (query == null)
            {
                query = CreateSqlText(db, type, actionName);
                _actionSqlQueryInfo[key] = query;
            }

            return query;
        }

        public virtual List<T> SelectAll()
        {
            DbManager db = GetDbManager();

            try
            {
                return SelectAll(db);
            }
            finally
            {
                if (DisposeDbManager)
                    db.Dispose();
            }
        }

        static SqlQueryInfo _query;
        public virtual List<T> SelectAll(DbManager db)
        {
            if (_query == null)
                _query = GetSqlQueryInfo(db, typeof(T), "SelectAll");

            return new List<T>();//db
                //.SetCommand(_selectAllQuery.QueryText)
                //.ExecuteList<T>();
        }


        private DbManager _dbManager;
        protected DbManager DbManager
        {
            get { return _dbManager; }
        }
        [System.Diagnostics.DebuggerStepThrough]
        public virtual DbManager GetDbManager()
        {
            return _dbManager ?? CreateDbManager();
        }

        protected virtual DbManager CreateDbManager()
        {
            return new DbManager();
        }
        protected virtual string GetTableName(Type type)
        {
            //bool isSet;
            //return MappingSchema.MetadataProvider.GetTableName(type, Extensions, out isSet);
            return "tableName";
        }

        protected virtual SqlQueryInfo CreateSqlText(DbManager db, Type type, string actionName)
        {
            return new SqlQueryInfo(db.MappingSchema.GetObjectMapper(type));
            switch (actionName)
            {
            //    case "SelectByKey": return CreateSelectByKeySqlText(db, type);
                case "SelectAll": 
                //return CreateSelectAllSqlText(db, type);
            //    case "Insert": return CreateInsertSqlText(db, type, -1);
            //    case "InsertBatch": return CreateInsertSqlText(db, type, 0);
            //    case "Update": return CreateUpdateSqlText(db, type, -1);
            //    case "UpdateBatch": return CreateUpdateSqlText(db, type, 0);
            //    case "Delete": return CreateDeleteSqlText(db, type, -1);
            //    case "DeleteBatch": return CreateDeleteSqlText(db, type, 0);
                default:
                    throw new AccessViolationException(
                        string.Format("Unknown action '{0}'.", actionName));
            }
        }

    }
}
